package com.cos.commons;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class R<T> {

    private static final String SUCCSES="succses";
    private static final String ERROR="error";

    private String status; //表示请求是否成功
    private  String mag;//如果请求失败 失败原因
    private T data; //响应的数据
    /**
     *
     * @Author lyc
     * @Description: 成功时用
     * @Return:
     * @Create: 2021/7/6 17:38
     */
    public static R success(){
        return  new R(SUCCSES,null,null);
    }
    public static R success(Object obj){
        return  new R(SUCCSES,null,obj);
    }
    //失败时用
    public static  R error(String mag){
        return  new R(ERROR,mag,null);
    }
    //公共响应的方法
    public static  R write(Object target){
        if (target instanceof  Boolean) {
            Boolean flag= (Boolean) target;
            if (flag) {
                return success();
            }else {
                return error("失败");
            }
        }
        return error(null);
    }
}
